﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Data;

/// <summary>
///unit 的摘要说明
/// </summary>
public class unit
{
	public unit()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    static JavaScriptSerializer serializer = new JavaScriptSerializer();
    static sqlhelp sql = new sqlhelp();

    public static string getImg(string linkID) {
        string imgPath = "";
        DataTable dt = sql.myDataSet("select * from SYS_Image where Linkid = '" + linkID + "'").Tables[0];
        if (dt.Rows.Count > 0) {
            imgPath = dt.Rows[0]["Path"].ToString();
        }
        return imgPath;
    }

    public static void send(HttpContext context,result res)
    {
        string resultstr = serializer.Serialize(res);
        context.Response.ContentType = "text/plain";
        context.Response.Write(resultstr);
    }

    public static string initCode()
    {
        string _code = GetRecordId(6);
        while (codeInDb(_code)) {
            _code = GetRecordId(6);
        }
        return _code;
    }

    public static bool codeInDb(string code) {
        DataTable dt = sql.myDataSet("select Id from SYS_Store where Code = '"+code+"'").Tables[0];
        if (dt.Rows.Count > 0) {
            return true;
        }
        return false;
    }



    public static string GetRecordId(int count)
        {
            Random rdn = new Random();
            string str = "";
            string[] arraylist = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
            for (int i = 0; i < count; i++)
            {
                int number = rdn.Next(0, 10);
                if (i == 0)
                {
                    number = rdn.Next(1, 10);
                    str = arraylist[number];
                }
                else
                {

                    str += arraylist[number];
                }
            }

            return str;
        }
}